$(function () {
    $.ajax({
        url: "http://localhost:8848/getborrowinfo.php",
        type: "GET",
        data: {
            borrowid: sessionStorage.getItem("borrowid")
        },
        dataType: "JSON",
        success: function (data) {
            console.log(data)
            for (i in data) {
                if (data[i] == 0) {
                    $("." + i).html("按月分期")
                } else if (data[i] == 1) {
                    $("." + i).html("按月到期")
                } else {
                    $("." + i).html(data[i])
                }

            }

            drawImages(data.borrowmoney, data.ownmoney, data.userid);
        }
    })
    // 画图echarts
    function drawImages(borrowmoney, ownmoney, userid) {
        // 基于准备好的dom，初始化echarts实例
        var myChart = echarts.init(document.getElementById('main'));
        // 指定图表的配置项和数据
        var option = {
            title: {
                text: '借款进度对比',
                subtext: userid,
                left: 'center'
            },
            tooltip: {
                trigger: 'item'
            },
            legend: {
                orient: 'vertical',
                left: 'left'
            },
            series: [
                {
                    name: 'Access From',
                    type: 'pie',
                    radius: '50%',
                    data: [
                        { value: borrowmoney, name: '借款金额' },
                        { value: ownmoney, name: '已有金额' },

                    ],
                    emphasis: {
                        itemStyle: {
                            shadowBlur: 10,
                            shadowOffsetX: 0,
                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                        }
                    }
                }
            ]
        };

        // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option);

    }

    $("#invest").click(function () {
        $.ajax({
            url: "http://127.0.0.1:8848/invest.php",
            type: "POST",
            data: {
                chargemoney: $("#investmoney").val(),
                id: localStorage.getItem("uid"),
                borrowid: sessionStorage.getItem("borrowid"),

            },
            success: function (data) {
                if (data == "10001") {
                    alert("您的余额不足,请充值。")
                    location.href = "/charge.html"
                    // location.href = "/charge.html"
                } else if (data == "10002") {
                    alert("投资失败。")
                } else if (data == "10003") {
                    alert("扣钱失败")
                } else {
                    alert("投资成功")

                }
            }
        })

    })
})